<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\common\model\CustomerPrice as Model;
use think\facade\Log;

class Price extends AdminBase
{
    /**
     * 用户列表
     */
    public function index($limit='10')
    {
        $model = new Model();
        $param = $this->request->param();
        if(!empty($param['keyword'])){
            $model = $model->where('name|mobile','like','%'.$param['keyword'].'%');
        }
        if(!empty($param['customer_id'])){
            $model = $model->where('customer_id',$param['customer_id']);
        }
        $lists = $model->with(['customer','type','user','hetong'])->order('id desc')->paginate($limit);
        $this->result(compact('lists'));
    }

    /**
     * 读取列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get_list()
    {
        $model = new Model();
        $lists = $model->order('id desc')->select();
        $this->result($lists);
    }

    /**
     * 查询详情
     * @param $id
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail($id)
    {
        $model = new Model();
        $data = $model->with(['from','trade','user','kefu'])->find($id);
        $this->result(compact('data'));
    }

    /**
     * 修改客户
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['user_id'] = AdminId();
            $model = new Model();
            if(!empty($param['id'])){
                unset($param['create_time']);
                $model = Model::find($param['id']);
            }
            $result = $model->save($param);
            $this->result('',0,"操作成功");
        }
    }

    /**
     * 删除用户
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            Model::destroy($param['id']);
            $this->result('', 0, "删除成功");
        }
    }

    /**
     * 删除用户
     */
    public function batch()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            Model::destroy($param['ids']);
            $this->result('',0,"删除成功");
        }
    }


}